<template>
  <div class="charge">
    <div class="phone">
      <input type v-model="phone" maxlength="11">
      <i v-if="isPhone" class="iconfont icon-ren"></i>
      <i v-else class="iconfont icon-guanbi" style="color:#898989" @click="phone=''"></i>
    </div>
    <div class="content">
      <div class="title">充话费</div>
      <ul>
        <li
          :class="{'LiActive':isPhone,'LiActiveItem':index==currentIdx}"
          v-for="(li ,index) in phoneFeesListArr"
          :key="index"
          @click="clickLi(li,index)"
        >
          <span class="money">{{li.value2}} 元</span>
          <span class="ps">{{li.value3}}PS</span>
        </li>
      </ul>
    </div>
    <div class="re_mask" v-if="isRechargeMask">
      <div class="main">
        <div class="title">
          <i class="iconfont icon-guanbi" @click="isRechargeMask=false;currentIdx=-1"></i>
          确认充值
        </div>
        <p class="psNumber">PS {{currentLi.value3}}</p>
        <p class="txt">付款方式 &#x3000;&#x3000; 账户余额</p>
        <input type="password" placeholder="请输入用户密码" v-model="password">
        <div class="btn" @click="rechargeNow">立即充值</div>
      </div>
    </div>
    <!-- 支付密码 -->
    <div class="payMask" v-if="isPayMask">
      <div class="mian">
        <div class="title">
          <i @click="isPayMask=false" class="iconfont icon-guanbi"></i> 请输入支付密码
        </div>
        <div class="content1">
          <div class="p">向{{this.phone}}充值{{currentLi.value2}}元话费</div>
          <div class="txt">
            <span class="ps">PS</span>
            <span>{{currentLi.value3}}</span>
          </div>
        </div>
        <div class="inputBox inputBox1" ref="inputBox">
          <!-- <input type="password" v-model="pws1" maxlength="1" @keydown="key_dow">
          <input type="password" v-model="pws2" maxlength="1">
          <input type="password" v-model="pws3" maxlength="1">
          <input type="password" v-model="pws4" maxlength="1">
          <input type="password" v-model="pws5" maxlength="1">
          <input type="password" v-model="pws6" maxlength="1">-->
          <input type="password" maxlength="6" v-model="passwordVal">
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { telephoneFees, getTelephoneFeesList } from "@/api.js";
import { getGuid } from "@/utils/index";
import { Toast } from "mint-ui";
import { hex_md5 } from "../../../utils/md5";
export default {
  name: "charge",
  data() {
    return {
      phone: "",
      phoneFeesListArr: [],
      id: localStorage.zyUserId,
      currentLi: {},
      password: "",
      isRechargeMask: false,
      currentIdx: -1,
      isPayMask: false,
      pwsVal: "",
      pws1: "",
      pws2: "",
      pws3: "",
      pws4: "",
      pws5: "",
      pws6: "",
      passwordVal: ""
    };
  },
  mounted() {
    this.chargeTelephoneFees();
    this.getTelephoneFees();
  },
  computed: {
    isPhone() {
      let len = this.phone.length == "11" ? true : false;
      return len;
    }
  },
  watch: {
    passwordVal(v1, v2) {
      if (v1.length == 6) {
        this.rechargeNow();
        console.log("输入完成");
      }
    }
  },
  methods: {
    inputWrite(txts) {
      if (!this.$refs.inputBox) {
        return;
      }
      var txts = this.$refs.inputBox.getElementsByTagName("input");
      let _this = this;
      for (var i = 0; i < txts.length; i++) {
        var t = txts[i];
        t.index = i;
        t.setAttribute("readonly", true);
        t.onkeyup = function() {
          this.value = this.value.replace(/^(.).*$/, "$1");
          var next = this.index + 1;
          if (next > txts.length - 1) {
            console.log("提交");
            _this.rechargeNow();
          }
          if (txts[next] == undefined) {
            return;
          }
          txts[next].removeAttribute("readonly");
          txts[next].focus();
        };
      }
      txts[0].removeAttribute("readonly");
    },
    key_dow() {
      this.inputWrite();
    },
    getVal() {
      this.pwsVal =
        this.pws1 + this.pws2 + this.pws3 + this.pws4 + this.pws5 + this.pws6;
    },
    getTelephoneFees() {
      getTelephoneFeesList({
        ID: this.id,
        LoginMachine: getGuid()
      }).then(res => {
        if (res.data.Success) {
          this.phoneFeesListArr = res.data.EntityList;
        }
      });
    },
    chargeTelephoneFees({ YJELQss, YJEYhOld, MC, JYEPass } = { YJELQss: "0" }) {
      telephoneFees({
        ID: this.id,
        LoginMachine: getGuid(),
        YJELQss: YJELQss,
        YJEYhOld: YJEYhOld,
        MC: MC,
        JYEPass: JYEPass
      }).then(res => {
        if (res.data.Success) {
          if (YJELQss == 0) {
            this.phone = res.data.Content;
          } else {
            Toast({
              message: res.data.Content,
              duration: 2000
            });
          }
        } else {
          Toast({
            message: res.data.Content,
            duration: 2000
          });
        }
        this.isRechargeMask = false;
      });
    },
    clickLi(i, idx) {
      this.passwordVal = "";
      if (this.phone.length != 11) {
        Toast({
          message: "请输入正确的手机号",
          duration: 2000
        });
        return;
      }
      this.password = "";
      this.currentIdx = idx;
      this.currentLi = i;
      setTimeout(() => {
        // this.isRechargeMask = true;
        this.isPayMask = true;
      }, 200);
    },
    rechargeNow() {
      this.isPayMask = false;
      this.chargeTelephoneFees({
        YJELQss: "1",
        YJEYhOld: this.phone,
        MC: this.currentLi.value1,
        JYEPass: hex_md5(this.passwordVal)
      });
    }
  }
};
</script>
<style lang="stylus" scoped>
.charge {
  .payMask {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    position: fixed;
    top: 0;
    left: 0;
    z-index: 999;

    .mian {
      width: 7.04rem;
      height: 7.1rem;
      background: #FFF;
      border-radius: 0.2rem;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      text-align: center;

      .title {
        width: 6.67rem;
        line-height: 1.08rem;
        border-bottom: 1px solid rgba(230, 230, 230, 1);
        margin: 0 auto;
        font-size: 16px;
        font-weight: 500;

        i {
          float: left;
        }
      }

      .content1 {
        width: 6.63rem;
        height: 3rem;
        margin: 0.5rem auto;
        border-bottom: 1px solid rgba(137, 137, 137, 1);
        text-align: center;
        box-sizing: border-box;
        padding-top: 0.5rem;

        .p {
          margin: 0.3rem auto;
          font-size: 14px;
        }

        .txt {
          font-weight: 500;

          .ps {
            font-size: 30px;
            display: inline-block;
            position: relative;
            bottom: 5px;
          }

          span {
            font-size: 40px;
          }
        }
      }

      .inputBox {
        width: 6.32rem;
        height: 0.93rem;
        display: flex;
        margin: 0.07rem auto;

        input {
          width: 1.03rem;
          flex: 1;
          border: 1px solid rgba(203, 203, 203, 1);
          text-align: center;
        }
      }

      .inputBox1 {
        input {
          width: 100%;
          text-align: left;
          letter-spacing: 0.81rem;
          padding-left: 0.5rem;
        }
      }
    }
  }

  .phone {
    width: 7.04rem;
    height: 1.84rem;
    line-height: 1.84rem;
    background: #FFF;
    border-radius: 0.2rem;
    margin: 0 auto;
    padding: 0 15px;
    box-sizing: border-box;
    font-size: 31px;

    input {
      width: 90%;
    }

    i {
      font-size: 31px;
      color: #57C10C;
      float: right;
      margin-top: 6px;
    }
  }

  .content {
    width: 7.04rem;
    height: 7.65rem;
    background: #FFF;
    border-radius: 0.2rem;
    margin: 10px auto;

    .title {
      line-height: 1rem;
      font-size: 19px;
      margin-left: 10px;
    }

    ul {
      width: 100%;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      align-items: center; /* 垂直居中 */
      justify-content: center; /* 水平居中 */

      li {
        width: 2.12rem;
        height: 1.39rem;
        border: 1px solid #898989;
        margin: 10px 2px;
        display: flex;
        flex-direction: column;
        align-items: center; /* 垂直居中 */
        justify-content: center; /* 水平居中 */
        border-radius: 0.1rem;
        color: #898989;

        .money {
          font-size: 19px;
          line-height: 0.5rem;
        }

        .ps {
          font-size: 14px;
          line-height: 0.5rem;
        }
      }

      .LiActive {
        border: 1px solid #57C10C;
        color: #57C10C;
      }

      .LiActiveItem {
        background: #57C10C;
        color: #fff;
      }
    }
  }

  .re_mask {
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.9);
    position: fixed;
    left: 0;
    top: 0;

    // display: none;
    .main {
      width: 7.04rem;
      height: 8.25rem;
      background: #FFF;
      border-radius: 0.2rem;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      text-align: center;

      .title {
        width: 6.63rem;
        line-height: 1.11rem;
        border-bottom: 1px solid rgba(137, 137, 137, 0.6);
        margin: 0 auto;
        font-size: 16px;

        i {
          position: absolute;
          left: 15px;
          top: 5px;
          font-size: 18px;
        }
      }

      .psNumber {
        font-size: 33px;
        margin: 50px auto 20px auto;
      }

      .txt {
        font-size: 14px;
        color: #898989;
        margin: 20px auto;
      }

      input {
        width: 5.79rem;
        height: 0.88rem;
        background: rgba(137, 137, 137, 0.6);
        margin: 30px auto;
        border-radius: 5px;
        padding: 0 5px;
        // box-sizing: border-box;
      }

      .btn {
        width: 5.79rem;
        line-height: 1.01rem;
        background: rgba(255, 99, 44, 1);
        border-radius: 5px;
        color: #fff;
        margin: 30px auto 20px auto;
        font-size: 20px;
      }
    }
  }
}
</style>

